from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json
from typing import List, Optional

mcp = FastMCP("Batik MCP Server")

"""
Batik MCP Server
面向软件：batik（特别是其中的 ttf2svg 工具）

MCP tools 列表：
1. ttf2svg_convert - 将 TTF 字体文件转换为 SVG 格式
   参数：input_file (str), output_file (str)
   返回：统一的命令执行结果 JSON

2. ttf2svg_help - 显示 ttf2svg 的帮助信息
   参数：无
   返回：统一的命令执行结果 JSON

3. ttf2svg_convert_character - 转换特定字符为 SVG
   参数：input_file (str), output_file (str), low_char (int), high_char (int)
   返回：统一的命令执行结果 JSON

4. ttf2svg_convert_with_id - 使用自定义字体 ID 进行转换
   参数：input_file (str), output_file (str), font_id (str)
   返回：统一的命令执行结果 JSON
"""

@mcp.tool()
def ttf2svg_convert(input_file: str, output_file: str) -> dict:
    """
    将 TTF 字体文件转换为 SVG 格式
    
    参数:
        input_file: 输入的 TTF 字体文件路径
        output_file: 输出的 SVG 文件路径
    
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["ttf2svg", input_file, "-o", output_file]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def ttf2svg_help() -> dict:
    """
    显示 ttf2svg 的帮助信息
    
    参数:
        无
    
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["ttf2svg", "--help"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def ttf2svg_convert_character(input_file: str, output_file: str, low_char: int, high_char: int) -> dict:
    """
    转换特定字符为 SVG，支持指定字符范围
    
    参数:
        input_file: 输入的 TTF 字体文件路径
        output_file: 输出的 SVG 文件路径
        low_char: 起始字符的 ASCII 码
        high_char: 结束字符的 ASCII 码
    
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["ttf2svg", input_file, "-o", output_file, "-l", str(low_char), "-h", str(high_char)]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def ttf2svg_convert_with_id(input_file: str, output_file: str, font_id: str) -> dict:
    """
    使用自定义字体 ID 进行转换
    
    参数:
        input_file: 输入的 TTF 字体文件路径
        output_file: 输出的 SVG 文件路径
        font_id: 自定义字体 ID
    
    返回:
        JSON 结构: {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["ttf2svg", input_file, "-o", output_file, "-id", font_id]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()